package servlet;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import dao.UserAddressDao;
import pojo.Product;
import pojo.UserAddress;
import service.ProductService;
import service.UserAddressService;

/**
 * Servlet implementation class UserAddress
 */
@WebServlet("/UserAddressServlet")
public class UserAddressServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserAddressServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		String method=request.getParameter("method");
		if(method.equals("add")) {
			add(request,response);
		}else if(method.equals("del")) {
			del(request,response);
		}else if(method.equals("update")) {
			update(request,response);
		}else if(method.equals("up")) {
			up(request,response);
		}
	}

	private void up(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		try {
			request.setCharacterEncoding("utf-8");
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		int uid=Integer.parseInt(request.getParameter("uid"));
		UserAddressService use=new  UserAddressService() ;
			UserAddress u=use.getUserAddress(uid);
		if(u!=null) {
			HttpSession session=request.getSession(true);
			session.setAttribute("U", u);
			try {
				response.sendRedirect("updateAddress.jsp");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	private void update(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		UserAddress u = new UserAddress();
		int uid=Integer.parseInt(request.getParameter("uid"));
		u.setUid(uid);
		u.setName(request.getParameter("name"));
		u.setTel(request.getParameter("tel"));
		u.setAddress(request.getParameter("address"));
		UserAddressService use=new UserAddressService();
		int i=use.update(u);
		if(i>0){
			try {
				response.sendRedirect("order.jsp");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			}else{	
				try {
					response.sendRedirect("fail.jsp");
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
	}

	private void del(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		int uid=Integer.parseInt(request.getParameter("uid"));
		UserAddressService use=new UserAddressService();
		int i=use.del(uid);
		if(i>0) {
			try {
				response.sendRedirect("order.jsp");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	private void add(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		UserAddress u = new UserAddress();
		String c_name=request.getParameter("c_name");
		u.setC_name(c_name);
		u.setName(request.getParameter("name"));
		u.setTel(request.getParameter("tel"));
		u.setAddress(request.getParameter("address"));
		UserAddressService use=new UserAddressService();
		int i=use.add(u);
		if(i>0){
			try {
				response.sendRedirect("confirm.jsp");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			}else{	
				try {
					response.sendRedirect("fail.jsp");
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
	}

}

